<?php

namespace Beerus\Modules\BrandRights\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Biz\model\brandPack\BrandPackageRightsLog;
use Biz\model\brandRights\BrandRightsSource;
use Biz\service\BrandRights\BrandPackageService;
use Biz\service\BrandRights\BrandRightsSourceService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class PackageRightsLogController extends Controller
{
    public const  PAGE_SIZE = 20;

    /**
     * 获取列表
     * @param Request $request
     * @param Content $content
     * @return Content
     */
    public function index(Request $request, Content $content): Content
    {
        $params = $request->all();
        $page_size = $params['per_page'] ?? self::PAGE_SIZE;
        $params['per_page'] = $page_size;

        $where = $this->_getWhere($params);

        $list = BrandPackageRightsLog::selectList($where);

        unset($where['LIMIT']);

        $count = BrandPackageRightsLog::selectCount($where);

        $packages = BrandPackageService::getList([]);
        $packagesArr = array_column($packages, 'package_name', 'id');

        $rights = BrandRightsSourceService::getList([]);
        $rightsArr = array_column($rights, 'rights_title', 'id');
        $rightsTypeArr = array_column($rights, 'rights_type', 'id');

        $gridModel = new GridDataModel($list, $count, [
            'perPage' => $page_size,

        ]);

        $grid = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $packagesArr, $rightsArr, $rightsTypeArr) {
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function ($filter) use ($packagesArr, $rightsArr) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 3, function ($filter) use ($packagesArr) {
                        $filter->equal('package_id', '套餐')->select($packagesArr);
                        $types = [
                            BrandPackageRightsLog::TYPE_ADD => '开启',
                            BrandPackageRightsLog::TYPE_CLOSE => '关闭',
                            BrandPackageRightsLog::TYPE_CHANGE_RIGHTS_NUM => '修改数量',
                        ];
                        $filter->equal('type', '操作类型')->select($types);
                    });
                    $filter->column(1 / 3, function ($filter) use ($rightsArr) {
                        $filter->equal('rights_id', '权益')->select($rightsArr);
                        $filter->equal('rights_name', '权益标识')->placeholder('权益标识');
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('admin_id', '管理员ID')->placeholder('管理员ID');
                    });
                });
                $grid->disableCreateButton();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('package_id', '套餐')->display(function ($package_id) use ($packagesArr) {
                    return $packagesArr[$package_id] ?? '';
                });
                $grid->column('rights_id', '权益')->display(function ($rights_id) use ($rightsArr) {
                    return $rightsArr[$rights_id] ?? '';
                });
                $grid->column('rights_name', '权益标识');
                $grid->column('rights_num', '权益数量')->display(function ($rights_num) use ($rightsTypeArr) {
                    if ($this->type == BrandPackageRightsLog::TYPE_CHANGE_RIGHTS_NUM) {
                        return $rights_num;
                    } elseif ($this->type == BrandPackageRightsLog::TYPE_ADD && $rightsTypeArr[$this->rights_id] == BrandRightsSource::RIGHTS_TYPE_LIMIT_NUM) {
                        return $rights_num;
                    } else {
                        return "";
                    }
                });
                $grid->column('type', '操作类型')->display(function ($type) {
                    $types = [
                        BrandPackageRightsLog::TYPE_ADD => '开启',
                        BrandPackageRightsLog::TYPE_CLOSE => '关闭',
                        BrandPackageRightsLog::TYPE_CHANGE_RIGHTS_NUM => '修改数量',
                    ];
                    return $types[$type] ?? '';
                });
                $grid->column('admin_id', '管理员')->display(function ($admin_id) {
                    return Admin::user()->find($admin_id)->name;
                });
                $grid->column('addtime', '操作时间')->display(function ($addtime) {
                    return date('Y-m-d H:i:s', $addtime);
                });
                $grid->disableActions();
            });

        $content->row($grid);

        return $content
            ->header('套餐权益修改记录')
            ->description(' ')
            ->breadcrumb(['text' => '套餐权益修改记录', 'url' => '/packageRightsLog/index']);
    }

    private function _getWhere($params): array
    {
        $packageId = $params['package_id'] ?? null;
        $rightsId = $params['rights_id'] ?? null;
        $rightsName = $params['rights_name'] ?? null;
        $adminId = $params['admin_id'] ?? null;
        $type = $params['type'] ?? null;
        $page = $params['page'] ?? 1;
        $per_page = $params['per_page'] ?? 20;
        if ($packageId) {
            $where['package_id'] = $packageId;
        }
        if ($rightsId) {
            $where['rights_id'] = $rightsId;
        }
        if ($rightsName) {
            $where['rights_name[~]'] = $rightsName . '%';
        }
        if ($adminId) {
            $where['admin_id'] = $adminId;
        }
        if ($type) {
            $where['type'] = $type;
        }
        $where['LIMIT'] = [
            ($page - 1) * $per_page,
            $per_page,
        ];
        $where['ORDER'] = [
            'id' => 'ASC'
        ];
        return $where;
    }
}